home *** CD-ROM | disk | FTP | other *** search
/ ADA Programming Guide / ADA Programming Guide.iso / ada_gwu / pspans.c < prev    next >
C/C++ Source or Header  |  1996-01-30  |  5KB  |  187 lines

  1. /*
  2.  * Copyright (C) 1985-1992  New York University
  3.  * 
  4.  * This file is part of the Ada/Ed-C system.  See the Ada/Ed README file for
  5.  * warranty (none) and distribution info and also the GNU General Public
  6.  * License for more details.
  7.  
  8.  */
  9.  
  10. #include "hdr.h"
  11. #include "vars.h"
  12. #include "ada.h"
  13. #include "miscp.h"
  14. #include "adalexp.h"
  15. #include "pspansp.h"
  16.  
  17. extern char *emalloc();
  18.  
  19. int is_terminal_node_p(short node_kind)                    /*;is_terminal_node_p*/
  20. {
  21.     return (
  22.         node_kind == as_string ||
  23.         node_kind == as_string_literal ||
  24.         node_kind == as_character_literal ||
  25.         node_kind == as_int_literal ||
  26.         node_kind == as_real_literal ||
  27.         node_kind == as_simple_name ||
  28.         node_kind == as_operator ||
  29.         node_kind == as_mode ||
  30.         node_kind == as_package_stub ||
  31.         node_kind == as_task_stub ||
  32.         node_kind == as_null ||
  33.         node_kind == as_null_s ||
  34.         node_kind == as_others ||
  35.         node_kind == as_generic ||
  36.         node_kind == as_line_no 
  37.         );
  38. }
  39.  
  40. Span get_left_span_p(Node node)            /*;get_left_span_p*/
  41. {
  42.     short nkind,first;
  43.     Node firstelem;
  44.  
  45.     nkind = N_KIND(node);
  46.     if (is_terminal_node_p(nkind))
  47.         return N_SPAN(node);
  48.     else if (nkind == as_opt)
  49.         return N_SPAN(node);
  50.     else if (nkind == as_exit) {
  51.         return get_left_span_p(N_AST4(node));
  52.     }
  53.     else if (nkind == as_return) {
  54.         return get_left_span_p(N_AST4(node));
  55.     }
  56.     else if (nkind == as_raise) {
  57.         return get_left_span_p(N_AST2(node));
  58.     }
  59.     else if (nkind == as_others_choice) {
  60.         return get_left_span_p(N_AST3(node));
  61.     }
  62.     else if (islist_node[nkind]) {
  63.         if (!tup_size(N_LIST(node))) /* only for parser */
  64.             return N_SPAN(node);
  65.         else {
  66.             firstelem = (Node)N_LIST(node)[1];/* first list element */
  67.             if (firstelem == OPT_NODE)
  68.                 chaos("get_left_span_p: list element OPT_NODE");
  69.             return get_left_span_p(firstelem);
  70.         }
  71.     }
  72.     else if (isast_node[nkind]) {
  73.         firstelem = (Node)0;
  74.         if (N_AST1(node) != OPT_NODE && N_AST1(node) != (Node )0)
  75.             firstelem = N_AST1(node);
  76.         else if (N_AST2_DEFINED(nkind)) {
  77.             if (N_AST2(node) != OPT_NODE && N_AST2(node) != (Node )0)
  78.                 firstelem = N_AST2(node);
  79.             else if (N_AST3_DEFINED(nkind)) {
  80.                 if (N_AST3(node) != OPT_NODE && N_AST3(node) != (Node )0)
  81.                     firstelem = N_AST3(node);
  82.                 else if (N_AST4_DEFINED(nkind)) {
  83.                     if (N_AST4(node) != OPT_NODE && N_AST4(node) != (Node )0)
  84.                         firstelem = N_AST4(node);
  85.                 }
  86.             }
  87.         }
  88.         if (firstelem)
  89.             return get_left_span_p(firstelem);
  90.         else {
  91.             printf("node kind %d\n",nkind);
  92.             chaos("get_left_span_p: all ast's are OPT_NODE");
  93.         }
  94.     }
  95.     else printf("get_left_span_p: bad node kind %d\n",nkind);
  96.     chaos("get_left_span_p");
  97.     return (Span)0;
  98. }
  99.  
  100. Span get_right_span_p(Node node)        /*;get_right_span_p*/
  101. {
  102.     short nkind,last,length=1;
  103.     Node lastelem;
  104.  
  105.     nkind = N_KIND(node);
  106.     if (is_terminal_node_p(nkind)) {
  107.         if (isval_node[nkind])
  108.             /* as_null, as_null_s, as_others, as_others_choice
  109.              * have no N_VAL field defined
  110.              */
  111.             length = strlen(namelist(N_ID(node)));
  112.         return (make_span(N_SPAN0(node), N_SPAN1(node)+length-1));
  113.     }
  114.     else if (nkind == as_opt)
  115.         return N_SPAN(node);
  116.     else if (nkind == as_exit) {
  117.         if (N_AST2(node) != OPT_NODE)
  118.             return get_right_span_p(N_AST2(node));
  119.         else if (N_AST1(node) != OPT_NODE)
  120.             return get_right_span_p(N_AST1(node));
  121.         else return get_right_span_p(N_AST4(node));
  122.     }
  123.     else if (nkind == as_return) {
  124.         if (N_AST1(node) != OPT_NODE)
  125.             return get_right_span_p(N_AST1(node));
  126.         else return get_right_span_p(N_AST4(node));
  127.     }
  128.     else if (nkind == as_raise) {
  129.         if (N_AST1(node) != OPT_NODE)
  130.             return get_right_span_p(N_AST1(node));
  131.         else return get_right_span_p(N_AST2(node));
  132.     }
  133.     else if (nkind == as_others_choice) {
  134.         if (N_AST2(node) != OPT_NODE)
  135.             return get_right_span_p(N_AST2(node));
  136.         else if (N_AST1(node) != OPT_NODE)
  137.             return get_right_span_p(N_AST1(node));
  138.         else return get_right_span_p(N_AST3(node));
  139.     }
  140.     else if (islist_node[nkind]) {
  141.         if (!tup_size(N_LIST(node))) /* only for parser */
  142.             return N_SPAN(node);
  143.         else {
  144.             lastelem = (Node)N_LIST(node)[tup_size(N_LIST(node))];
  145. #ifdef TODO
  146.             -- this finds next element in list. we need to search backwards !!!
  147.                 while ((lastelem)->val.node == OPT_NODE) {
  148.                 lastelem = lastelem->link; /* last list element */
  149.                 if (lastelem == node->links.list)
  150.                     chaos("get_right_span_p: all list elements OPT_NODE");
  151.             }
  152. #endif
  153.             if (lastelem == OPT_NODE)
  154.                 chaos("get_right_span_p: list element OPT_NODE");
  155.             return get_right_span_p(lastelem);
  156.         }
  157.     }
  158.     else if (isast_node[nkind]) {
  159.         if (N_AST4_DEFINED(nkind) && N_AST4(node) != OPT_NODE 
  160.             && N_AST4(node) != (Node )0)
  161.             lastelem = N_AST4(node);
  162.         else if (N_AST3_DEFINED(nkind) && 
  163.             N_AST3(node) != OPT_NODE && N_AST3(node) != (Node )0)
  164.             lastelem = N_AST3(node);
  165.         else if (N_AST2_DEFINED(nkind) && 
  166.             N_AST2(node) != OPT_NODE && N_AST2(node) != (Node )0)
  167.             lastelem = N_AST2(node);
  168.         else if (N_AST1(node) == OPT_NODE && N_AST1(node) == (Node )0)
  169.             chaos("get_right_span_p: all ast's are OPT_NODE");
  170.         else lastelem = N_AST1(node);
  171.         return get_right_span_p(lastelem);
  172.     }
  173.     else printf("get_right_span_p: bad node kind %d\n",nkind);
  174.     chaos("get_right_span_p");
  175.     return (Span)0;
  176. }
  177.  
  178. Span make_span(short line, short col)            /*;make_span*/
  179. {
  180.     Span tok;
  181.  
  182.     tok = (Span) emalloc(sizeof(Span_s));
  183.     tok->line = line;
  184.     tok->col  = col;
  185.     return (tok);
  186. }
  187.